package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.api.vo.LogiLineExtVo;
import com.piggy.logi.sys.api.vo.LogiSysDeptExtVo;
import com.piggy.logi.sys.bo.LogiLineExtEditBo;
import com.piggy.logi.sys.bo.LogiLineQueryBo;
import com.piggy.logi.sys.service.ILogiLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 线路管理Controller
 *
 * @author piggy
 * @date 2023-08-14
 */
@Api(value = "线路管理控制器", tags = {"线路管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/line")
public class LogiLineController extends BaseController {

    private final ILogiLineService iLogiLineService;

    /**
     * 查询线路管理列表
     */
    @ApiOperation("查询线路管理列表")
    //@SaCheckPermission("sys:line:list")
    @PostMapping("/list")
    public TableDataInfo<LogiLineExtVo> list(@Validated @RequestBody LogiLineQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiLineService.queryPageList(bo);
    }

    /**
     * 导出线路管理列表
     */
    @ApiOperation("导出线路管理列表")
    //@SaCheckPermission("sys:line:export")
    @LogiLog(title = "线路管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiLineQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setDeptId(SecurityUtils.getDeptId());
        List<LogiLineExtVo> list = iLogiLineService.queryList(bo);
        ExcelUtil<LogiLineExtVo> util = new ExcelUtil<LogiLineExtVo>(LogiLineExtVo.class);
        util.exportExcel(response, list, "线路管理");
    }

    /**
     * 获取线路管理详细信息
     */
    @ApiOperation("获取线路管理详细信息")
    //@SaCheckPermission("sys:line:query")
    @GetMapping("/{lineId}")
    public R<LogiLineExtVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("lineId") Long lineId) {
        return R.ok(iLogiLineService.queryById(lineId));
    }

    /**
     * 新增线路管理
     */
    @ApiOperation("新增线路管理")
    //@SaCheckPermission("sys:line:add")
    @LogiLog(title = "线路管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<LogiLineExtVo> add(@Validated @RequestBody LogiLineExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiLineService.insertByAddBo(bo));
    }

    /**
     * 修改线路管理
     */
    @ApiOperation("修改线路管理")
    //@SaCheckPermission("sys:line:edit")
    @LogiLog(title = "线路管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<LogiLineExtVo> edit(@Validated @RequestBody LogiLineExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiLineService.updateByEditBo(bo));
    }

    /**
     * 删除线路管理
     */
    @ApiOperation("删除线路管理")
    //@SaCheckPermission("sys:line:remove")
    @LogiLog(title = "线路管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{lineIds}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] lineIds) {
        return R.ok(iLogiLineService.deleteWithValidByIds(Arrays.asList(lineIds), true) ? 1 : 0);
    }

    @ApiOperation("根据(出发地址)经纬度获取线路信息")
    @GetMapping("/geo")
    public List<LogiLineExtVo> geo(@RequestParam("lng") BigDecimal lng, @RequestParam("lat") BigDecimal lat) {
        return iLogiLineService.selectGeoDeptList(lng, lat);
    }

}
